package com.zhida.smartcontrol.pagehome.search;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.jay.tools.recycler.OnItemClick;
import com.jay.tools.utils.EventBusUtils;
import com.jay.tools.utils.SoftInputUtils;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.mvp.MVPBaseFragment;
import com.zhida.smartcontrol.pagehome.ProjectResultBean;
import com.zhida.smartcontrol.pagehome.SearchBackEvent;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author: tanjianpei
 * date: 2019/10/10 010 14:44
 * description: 搜索
 */
public class SearchFragment extends MVPBaseFragment<SearchContract.View, SearchPresenter> implements SearchContract.View {
    @BindView(R.id.et_search)
    EditText mEtSearch;
    @BindView(R.id.recycler_view)
    RecyclerView mRView;

    private String mId;
    private SearchAdapter mAdapter;

    public static SearchFragment newInstance(String id) {
        Bundle args = new Bundle();
        args.putString("id", id);
        SearchFragment fragment = new SearchFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Object getLayout() {
        return R.layout.fragment_search;
    }

    @Override
    protected void initLazy(Bundle savedInstanceState) {
        initRecycler(mRView, 1, R.color.c_EEEEEE);

        mId = getArguments().getString("id");

        mAdapter = new SearchAdapter(null);
        mAdapter.bindToRecyclerView(mRView);

        mEtSearch.requestFocus();
        SoftInputUtils.showSoftInput(getProxyActivity(), mEtSearch);
    }

    @OnClick({R.id.tv_cancel, R.id.iv_search})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.iv_search:
                String key = mEtSearch.getText().toString().trim();
                mPresenter.requestByProjectAndKeyWord(mId, key);
                break;
            case R.id.tv_cancel:
                pop();
                break;
        }
    }

    @Override
    protected void initLazyEvents() {
        mEtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String key = mEtSearch.getText().toString().trim();
                    mPresenter.requestByProjectAndKeyWord(mId, key);
                    return true;
                }
                return false;
            }
        });

        mAdapter.setClickListener(new OnItemClick<ProjectResultBean>() {
            @Override
            public void onClick(ProjectResultBean info, int tag) {
                EventBusUtils.post(new SearchBackEvent(info));
                pop();
            }
        });
    }

    @Override
    public void showList(List<ProjectResultBean> resultBeans) {
        SoftInputUtils.hideSoftInput(getProxyActivity(), mEtSearch);
        mAdapter.replaceData(resultBeans);
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        stateBar(true);
    }

    @Override
    public void onSupportInvisible() {
        super.onSupportInvisible();
        stateBar(false);
    }

}
